{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 参数初始化\n",
    "参数初始化对模型具有较大的影响，不同的初始化方式可能会导致截然不同的结果，所幸的是很多深度学习的先驱们已经帮我们探索了各种各样的初始化方式，所以我们只需要学会如何对模型的参数进行初始化的赋值即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PyTorch 的初始化方式并没有那么显然，如果你使用最原始的方式创建模型，那么你需要定义模型中的所有参数，当然这样你可以非常方便地定义每个变量的初始化方式，但是对于复杂的模型，这并不容易，而且我们推崇使用 Sequential 和 Module 来定义模型，所以这个时候我们就需要知道如何来自定义初始化方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 NumPy 来初始化\n",
    "因为 PyTorch 是一个非常灵活的框架，理论上能够对所有的 Tensor 进行操作，所以我们能够通过定义新的 Tensor 来初始化，直接看下面的例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义一个 Sequential 模型\n",
    "net1 = nn.Sequential(\n",
    "    nn.Linear(30, 40),\n",
    "    nn.ReLU(),\n",
    "    nn.Linear(40, 50),\n",
    "    nn.ReLU(),\n",
    "    nn.Linear(50, 10)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 访问第一层的参数\n",
    "w1 = net1[0].weight\n",
    "b1 = net1[0].bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      " 0.1236 -0.1731 -0.0479  ...   0.0031  0.0784  0.1239\n",
      " 0.0713  0.1615  0.0500  ...  -0.1757 -0.1274 -0.1625\n",
      " 0.0638 -0.1543 -0.0362  ...   0.0316 -0.1774 -0.1242\n",
      "          ...             ⋱             ...          \n",
      " 0.1551  0.1772  0.1537  ...   0.0730  0.0950  0.0627\n",
      " 0.0495  0.0896  0.0243  ...  -0.1302 -0.0256 -0.0326\n",
      "-0.1193 -0.0989 -0.1795  ...   0.0939  0.0774 -0.0751\n",
      "[torch.FloatTensor of size 40x30]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(w1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意，这是一个 Parameter，也就是一个特殊的 Variable，我们可以访问其 `.data`属性得到其中的数据，然后直接定义一个新的 Tensor 对其进行替换，我们可以使用 PyTorch 中的一些随机数据生成的方式，比如 `torch.randn`，如果要使用更多 PyTorch 中没有的随机化方式，可以使用 numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 定义一个 Tensor 直接对其进行替换\n",
    "net1[0].weight.data = torch.from_numpy(np.random.uniform(3, 5, size=(40, 30)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      " 4.5768  3.6175  3.3098  ...   4.7374  4.0164  3.3037\n",
      " 4.1809  3.5624  3.1452  ...   3.0305  4.4444  4.1058\n",
      " 3.5277  4.3712  3.7859  ...   3.5760  4.8559  4.3252\n",
      "          ...             ⋱             ...          \n",
      " 4.8983  3.9855  3.2842  ...   4.7683  4.7590  3.3498\n",
      " 4.9168  4.5723  3.5870  ...   3.2032  3.9842  3.2484\n",
      " 4.2532  4.6352  4.4857  ...   3.7543  3.9885  4.4211\n",
      "[torch.DoubleTensor of size 40x30]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(net1[0].weight)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到这个参数的值已经被改变了，也就是说已经被定义成了我们需要的初始化方式，如果模型中某一层需要我们手动去修改，那么我们可以直接用这种方式去访问，但是更多的时候是模型中相同类型的层都需要初始化成相同的方式，这个时候一种更高效的方式是使用循环去访问，比如"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "for layer in net1:\n",
    "    if isinstance(layer, nn.Linear): # 判断是否是线性层\n",
    "        param_shape = layer.weight.shape\n",
    "        layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5, size=param_shape)) \n",
    "        # 定义为均值为 0，方差为 0.5 的正态分布"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**小练习：一种非常流行的初始化方式叫 Xavier，方法来源于 2010 年的一篇论文 [Understanding the difficulty of training deep feedforward neural networks](http://proceedings.mlr.press/v9/glorot10a.html)，其通过数学的推到，证明了这种初始化方式可以使得每一层的输出方差是尽可能相等的，有兴趣的同学可以去看看论文**\n",
    "\n",
    "我们给出这种初始化的公式\n",
    "\n",
    "$$\n",
    "w\\ \\sim \\ Uniform[- \\frac{\\sqrt{6}}{\\sqrt{n_j + n_{j+1}}}, \\frac{\\sqrt{6}}{\\sqrt{n_j + n_{j+1}}}]\n",
    "$$\n",
    "\n",
    "其中 $n_j$ 和 $n_{j+1}$ 表示该层的输入和输出数目，所以请尝试实现以下这种初始化方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于 Module 的参数初始化，其实也非常简单，如果想对其中的某层进行初始化，可以直接像 Sequential 一样对其 Tensor 进行重新定义，其唯一不同的地方在于，如果要用循环的方式访问，需要介绍两个属性，children 和 modules，下面我们举例来说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class sim_net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(sim_net, self).__init__()\n",
    "        self.l1 = nn.Sequential(\n",
    "            nn.Linear(30, 40),\n",
    "            nn.ReLU()\n",
    "        )\n",
    "        \n",
    "        self.l1[0].weight.data = torch.randn(40, 30) # 直接对某一层初始化\n",
    "        \n",
    "        self.l2 = nn.Sequential(\n",
    "            nn.Linear(40, 50),\n",
    "            nn.ReLU()\n",
    "        )\n",
    "        \n",
    "        self.l3 = nn.Sequential(\n",
    "            nn.Linear(50, 10),\n",
    "            nn.ReLU()\n",
    "        )\n",
    "    \n",
    "    def forward(self, x):\n",
    "        x = self.l1(x)\n",
    "        x =self.l2(x)\n",
    "        x = self.l3(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "net2 = sim_net()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequential(\n",
      "  (0): Linear(in_features=30, out_features=40)\n",
      "  (1): ReLU()\n",
      ")\n",
      "Sequential(\n",
      "  (0): Linear(in_features=40, out_features=50)\n",
      "  (1): ReLU()\n",
      ")\n",
      "Sequential(\n",
      "  (0): Linear(in_features=50, out_features=10)\n",
      "  (1): ReLU()\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "# 访问 children\n",
    "for i in net2.children():\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sim_net(\n",
      "  (l1): Sequential(\n",
      "    (0): Linear(in_features=30, out_features=40)\n",
      "    (1): ReLU()\n",
      "  )\n",
      "  (l2): Sequential(\n",
      "    (0): Linear(in_features=40, out_features=50)\n",
      "    (1): ReLU()\n",
      "  )\n",
      "  (l3): Sequential(\n",
      "    (0): Linear(in_features=50, out_features=10)\n",
      "    (1): ReLU()\n",
      "  )\n",
      ")\n",
      "Sequential(\n",
      "  (0): Linear(in_features=30, out_features=40)\n",
      "  (1): ReLU()\n",
      ")\n",
      "Linear(in_features=30, out_features=40)\n",
      "ReLU()\n",
      "Sequential(\n",
      "  (0): Linear(in_features=40, out_features=50)\n",
      "  (1): ReLU()\n",
      ")\n",
      "Linear(in_features=40, out_features=50)\n",
      "ReLU()\n",
      "Sequential(\n",
      "  (0): Linear(in_features=50, out_features=10)\n",
      "  (1): ReLU()\n",
      ")\n",
      "Linear(in_features=50, out_features=10)\n",
      "ReLU()\n"
     ]
    }
   ],
   "source": [
    "# 访问 modules\n",
    "for i in net2.modules():\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过上面的例子，看到区别了吗?\n",
    "\n",
    "children 只会访问到模型定义中的第一层，因为上面的模型中定义了三个 Sequential，所以只会访问到三个 Sequential，而 modules 会访问到最后的结构，比如上面的例子，modules 不仅访问到了 Sequential，也访问到了 Sequential 里面，这就对我们做初始化非常方便，比如"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "for layer in net2.modules():\n",
    "    if isinstance(layer, nn.Linear):\n",
    "        param_shape = layer.weight.shape\n",
    "        layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5, size=param_shape)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这上面实现了和 Sequential 相同的初始化，同样非常简便"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## torch.nn.init\n",
    "因为 PyTorch 灵活的特性，我们可以直接对 Tensor 进行操作从而初始化，PyTorch 也提供了初始化的函数帮助我们快速初始化，就是 `torch.nn.init`，其操作层面仍然在 Tensor 上，下面我们举例说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from torch.nn import init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      " 0.8453  0.2891 -0.5276  ...  -0.1530 -0.4474 -0.5470\n",
      "-0.1983 -0.4530 -0.1950  ...   0.4107 -0.4889  0.3654\n",
      " 0.9149 -0.5641 -0.6594  ...   0.0734  0.1354 -0.4152\n",
      "          ...             ⋱             ...          \n",
      "-0.4718 -0.5125 -0.5572  ...   0.0824 -0.6551  0.0840\n",
      "-0.2374 -0.0036  0.6497  ...   0.7856 -0.1367 -0.8795\n",
      " 0.0774  0.2609 -0.2358  ...  -0.8196  0.1696  0.5976\n",
      "[torch.DoubleTensor of size 40x30]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(net1[0].weight)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "-0.2114  0.2704 -0.2186  ...   0.1727  0.2158  0.0775\n",
       "-0.0736 -0.0565  0.0844  ...   0.1793  0.2520 -0.0047\n",
       " 0.1331 -0.1843  0.2426  ...  -0.2199 -0.0689  0.1756\n",
       "          ...             ⋱             ...          \n",
       " 0.2751 -0.1404  0.1225  ...   0.1926  0.0175 -0.2099\n",
       " 0.0970 -0.0733 -0.2461  ...   0.0605  0.1915 -0.1220\n",
       " 0.0199  0.1283 -0.1384  ...  -0.0344 -0.0560  0.2285\n",
       "[torch.DoubleTensor of size 40x30]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "init.xavier_uniform(net1[0].weight) # 这就是上面我们讲过的 Xavier 初始化方法，PyTorch 直接内置了其实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      "-0.2114  0.2704 -0.2186  ...   0.1727  0.2158  0.0775\n",
      "-0.0736 -0.0565  0.0844  ...   0.1793  0.2520 -0.0047\n",
      " 0.1331 -0.1843  0.2426  ...  -0.2199 -0.0689  0.1756\n",
      "          ...             ⋱             ...          \n",
      " 0.2751 -0.1404  0.1225  ...   0.1926  0.0175 -0.2099\n",
      " 0.0970 -0.0733 -0.2461  ...   0.0605  0.1915 -0.1220\n",
      " 0.0199  0.1283 -0.1384  ...  -0.0344 -0.0560  0.2285\n",
      "[torch.DoubleTensor of size 40x30]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(net1[0].weight)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到参数已经被修改了\n",
    "\n",
    "`torch.nn.init` 为我们提供了更多的内置初始化方式，避免了我们重复去实现一些相同的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面讲了两种初始化方式，其实它们的本质都是一样的，就是去修改某一层参数的实际值，而 `torch.nn.init` 提供了更多成熟的深度学习相关的初始化方式，非常方便\n",
    "\n",
    "下一节课，我们将讲一下目前流行的各种基于梯度的优化算法"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
